package com.bycx.core.util;

import org.apache.commons.id.Hex;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by yiqr on 2017/6/12.
 */
public class StringUtils{

    /**
     * 判断字符串是否不为空
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str){
        return !isEmpty(str);
    }

    /**
     * 判断字符串是否为空
     * @param str
     * @return
     */
    public static boolean isEmpty(String str){
        if (null == str || str.trim().length() == 0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 把逗号隔开的字符串转成LIST
     * @param orgStr String orgStr = "ID10002,ID10003,ID10004,ID10005,ID10006";
     * @return
     */
    public static List<String>  strSplit(String orgStr){
        if(StringUtils.isEmpty(orgStr)){
            return null;
        }
        String [] orgStrs = orgStr.split(",");
        return Arrays.asList(orgStrs);

    }
    /**
     * 生成UUID
     */
    public static String getUUID() {
        return new String(Hex.encodeHex(org.apache.commons.id.uuid.UUID.randomUUID().getRawBytes()));
    }

    /**
     *
     * @param prefix 格式化出来的String的抬头
     * @param no 在传入的这个no数字的基础上+1
     * @return
     */
    private static final String STR_FORMAT = "000000";
    public static String getFormtStr(String prefix,int no){
        no = no+1;
        DecimalFormat df = new DecimalFormat(STR_FORMAT);
        return prefix+df.format(no);
    }
    /**
     * List转逗号分隔字符串
     */
    @SuppressWarnings("rawtypes")
	public static String listToString(List list) {
        StringBuilder sb = new StringBuilder();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (i < list.size() - 1) {
                    sb.append(list.get(i) + ",");
                } else {
                    sb.append(list.get(i));
                }
            }
        }
        return sb.toString();
    }
    /**
     * 数组转逗号分隔字符串
     */
    public static String converToString(String[] ig) {
        String str = "";
        if (ig != null && ig.length > 0) {
            for (int i = 0; i < ig.length; i++) {
                str += ig[i] + ",";
            }
        }
        str = str.substring(0, str.length() - 1);
        return str;
    }

    /**
     * 去除字符串的回车换行
     * @param str
     * @return
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str!=null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
}
